// ==UserScript== // @name 修改 Yandex 搜索引擎的 cookie // @namespace https://scriptcat.org/zh-CN/users/157252 // @author deepseek // @version 1.3 // @icon https://yastatic.net/s3/home-static/_/nova/CKig_nVR.png // @description 自用脚本,为 Yandex 搜索选择性地注入 Cookie。 // @match http*://yandex.*/search/* // @run-at document-start // @license MIT // @grant none // ==/UserScript== (function() { 'use strict'; // 要注入的特定 Cookie 项目 const cookieEntries = { 'receive-cookie-deprecation': '1', 'gpb': 'yandex_gid.90#ygo.84%3A90#ygu.0', 'yandex_gid': '90', 'KIykI': '1', 'is_gdpr': '0', 'is_gdpr_b': 'CI2uORC1sQIoAg==', 'yp': '1742326362.sz.904x407x3#1756341914.szm.3:904x407:407x904#1758515894.ygu.0#1758515894.ygo.84:90#2055783435.pcs.1#1745523716.atds.1#1743252325.csc.1#1758515915.sp.family:0' }; // 智能提取主域名 const hostname = window.location.hostname; const parts = hostname.split('.'); const yandexIndex = parts.indexOf('yandex'); let dynamicDomain = ''; if (yandexIndex !== -1) { // 提取从 "yandex" 开始的部分(如 yandex.com.lv → .yandex.com.lv) dynamicDomain = '.' + parts.slice(yandexIndex).join('.'); } else { // 如果意外情况(根据 @match 规则应该不会触发) dynamicDomain = '.' + hostname; } // 获取当前 Cookie 并保留非冲突项 const currentCookies = Object.fromEntries( document.cookie.split(';') .map(cookie => cookie.trim().split(/=(.*)/, 2)) .filter(([name]) => name) ); // 安全更新 Cookie Object.entries(cookieEntries).forEach(([name, value]) => { if (currentCookies[name] !== value) { const isSecure = window.location.protocol === 'https:'; document.cookie = `${name}=${value}; domain=${dynamicDomain}; path=/; secure=${isSecure}; SameSite=Lax`; } }); // 调试输出 console.log('[Cookie 脚本] 最终 Cookie:', document.cookie); })();